



/**
 *  新增角色任务数据
 * @param characters_id 角色id
 * @param json_data 任务数据
 * @returns 角色信息
 */
export async function insert_characters_task(characters_id, all_task) {
    return await _database_.execute_insert(`
        INSERT INTO "characters_task" 
        (characters_id, json_data) 
        VALUES
        (
        '${characters_id}',
        '${JSON.stringify(all_task)}'
        );
    `)
};


/**
 *  查询数据库的角色任务数据
 * @param characters_id 角色id
 * @param json_data 任务数据 字符串
 * @returns 角色信息
 */
export async function get_characters_task(characters_id) {
    let res = await _database_.execute_query_one(
        `
        select * from characters_task where characters_id = ${characters_id}
        `
    );
    let task_obj = {};
    if (res !== "") {
        task_obj = JSON.parse(JSON.parse(res)["json_data"]);
    } else {
        await insert_characters_task(characters_id, {});
    }
    return task_obj;
};

/**
 *  持久化角色任务数据
 * @param characters_id 角色id
 * @param json_data 任务数据 字符串
 * @returns 角色信息
 */
export async function save_characters_task(characters_id) {
    let all_task = _CharactersTaskData.get(characters_id);
    await _database_.execute_update(`
        UPDATE "characters_task" SET json_data='` + JSON.stringify(all_task) + `' WHERE characters_id =` + characters_id + `;
    `);
};

/**
 * 加载角色任务数据
 * @param {*} characters_id 
 */
export async function load_characters_task(characters_id) {
    let all_task = await get_characters_task(characters_id);
    _CharactersTaskData.set(characters_id, all_task);
}

/**
 * 通知客户端刷新角色任务
 * @param {*} characters_id 
 */
export function refrush_task(characters_id) {
    let send_data = [];
    let all_task = _CharactersTaskData.get(characters_id);
    for (const key in all_task) {
        let task_data = all_task[key];
        if (task_data) {
            send_data.push(get_task_desc(key, task_data));
        }
    }
    ClientUtils.send_characters_client_message(characters_id, 46, send_data);
}

/**
 * 获取角色任务
 * @param {*} characters_id 
 * @param {*} task_number 
 * @returns 
 */
export function get_task_by_number(characters_id, task_number) {
    console.debug(`获取角色任务 => ${characters_id} , ${task_number}`)
    let all_task = _CharactersTaskData.get(characters_id);
    if (all_task) {
        return all_task[task_number];
    }
    return undefined;
}

/**
 * 通知客户端打开任务列表
 * @param {*} characters_id 
 */
export function send_task_list(characters_id) {
    let send_data = [];
    let all_task = _CharactersTaskData.get(characters_id);
    for (const key in all_task) {
        let task_data = all_task[key];
        if (task_data) {
            send_data.push(get_task_desc(key, task_data));
        }
    }
    ClientUtils.send_characters_client_message(characters_id, 40, send_data);
}

function get_task_desc(task_number, task_data) {
    let desc = [
        "未知任务",
        `无`,
        task_data["开始时间"],
        Math.floor((task_data["结束时间"] - Date.now() / 1000) / 60)
    ]
    // 取任务说明
    if (task_data["副本"]) {
        desc = _DungeonData.get(task_data["副本"])["任务说明"](task_data);
    } else if (task_desc[task_number]) {
        desc = task_desc[task_number](task_data);
    }
    return desc;
}

/**
 * 添加任务
 * @param {*} characters_id 
 * @param {*} task_number 
 * @param {*} task_data 
 */
export function add_task(characters_id, task_number, task_data) {
    let all_task = _CharactersTaskData.get(characters_id);
    if (all_task[task_number]) {
        canel_task(characters_id, task_number);
    }
    if (add_task_fn[task_number]) {
        add_task_fn[task_number](characters_id, task_data);
    }
    task_data["id"] = task_number;
    all_task[task_number] = task_data;
    // 刷新任务
    refrush_task(characters_id);
}

// 添加任务处理函数
const add_task_fn = {
    "1": (characters_id, task_data) => {
        let characters = _CharactersData.get(characters_id);
        //刷新任务
        refrush_task(characters["id"]);
        characters["变身数据"] = task_data["造型"];
        // 刷新属性
        G.Characters.refrush_characters_data(characters);
        // 通知更新模型
        ClientUtils.send_characters_client_message(characters["id"], 37, task_data["造型"]);
        // 刷新状态栏
        ClientUtils.send_characters_client_message(characters["id"], 12);
        ClientUtils.send_characters_basic_prompt(characters["id"], "#Y/你使用了怪物卡片");
        // 更新模型
        G.MapUtils.update_model(characters["id"], characters["变身数据"]);
    },
}

/**
 * 校验超时任务
 * @param {*} characters_id 
 */
export function check_timout_task(characters_id) {
    let all_task = _CharactersTaskData.get(characters_id);
    for (const task_number in all_task) {
        let task = all_task[task_number];
        if (task && task["超时取消"] && task["结束时间"] < Math.floor(Date.now() / 1000)) {
            if (task["副本"]) {
                _DungeonData.get(task["副本"])["超时处理"](task);
            }
            // 任务超时处理
            canel_task(characters_id, task_number);
        }
    }
}

/**
 * 取消任务
 * @param {*} characters_id 
 * @param {*} task_number 
 */
export function canel_task(characters_id, task_number) {
    let all_task = _CharactersTaskData.get(characters_id);
    if (canel_task_fn[task_number]) {
        canel_task_fn[task_number](characters_id, task_number);
    }
    all_task[task_number] = undefined;
    // 刷新任务
    refrush_task(characters_id);
}

// 取消任务处理函数
const canel_task_fn = {
    "1": (characters_id, task_data) => {
        let characters = _CharactersData.get(characters_id);
        characters["变身数据"] = undefined;
        ClientUtils.send_characters_basic_prompt(characters["id"], "#Y/你的变身时间到期了！");
        // 刷新属性
        G.Characters.refrush_characters_data(characters);
        // 通知更新模型
        ClientUtils.send_characters_client_message(characters["id"], 37, characters["造型"]);
        // 刷新状态栏
        ClientUtils.send_characters_client_message(characters["id"], 12);
        // 通知地图玩家更新模型
        G.MapUtils.update_model(characters["id"], characters["造型"]);
    }
}


/**
 * 任务详细说明
 */
export const task_desc = {
    "1": (task_data) => {
        return [
            "变身卡",
            `#L你的变身效果还可持续#R/ ${Math.floor((task_data["结束时间"] - Date.now() / 1000) / 60)} #L/分钟。`,
            null,
            Math.floor((task_data["结束时间"] - Date.now() / 1000) / 60)
        ];
    },
    "9": (task_data) => {
        return [
            "摄妖香",
            `#L你的摄妖香效果还可持续#R//${Math.floor((task_data["结束时间"] - Date.now() / 1000) / 60)}#L/分钟。在低于自身等级+10的场景中不会触发战斗。`,
            null,
            Math.floor((task_data["结束时间"] - Date.now() / 1000) / 60)
        ];
    },
    "10": (task_data) => {
        return [
            "罗羹效果",
            `#L/在每场战斗结束后都将自动恢复人物气血和魔法。当前还可恢复#R/${task_data["气血"]}#L点气血、当前还可恢复#R/${task_data["气血上限"]}#L点伤势、#R/${task_data["魔法"]}#L/点魔法`,
            null,
            [task_data["气血"], task_data["气血上限"], task_data["魔法"]]
        ];
    },
}